﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Xml.Linq;

namespace ApocDev.TownsApi
{
    public class TypesXml : List<Type>
    {
        private readonly string _filePath;
        public TypesXml(string path)
        {
            _filePath = path;
            var root = XElement.Load(path);
            foreach (var xml in root./*Element("types").*/Elements())
            {
                Add(new Type(xml));
            }
        }

        public void Save()
        {
            XElement root = new XElement("types");
            foreach (var type in this)
            {
                root.Add(type.Save());
            }
            root.Save(_filePath);
        }
    }

    public class Type
    {
        public string ID { get; set; }
        public string Name { get; set; }

        public Type(XElement xml)
        {
            ID = xml.Name.ToString();
            Name = xml.Value;
        }

        public XElement Save()
        {
            return new XElement(ID, Name);
        }
    }
}
